//
//  MoreSpecialTableViewController.m
//  GiftItems
//
//  Created by lanou3g on 15/9/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MoreSpecialTableViewController.h"
#import "MoreSpecialTableViewCell.h"
#import "MoreSpecialTwoTableViewCell.h"
#import "MainBoutiqueModel.h"

#import "BlurImageView.h"
#import "SpecialDetailsTableViewController.h"

@interface MoreSpecialTableViewController ()
@property (nonatomic,strong)NSMutableArray *specialArray;
@property (nonatomic,strong)NSString *NextUrl;
@property (nonatomic,strong)UIButton *toTopbutton;

@end

@implementation MoreSpecialTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self reloadSpeail:KRotateUrl];
    // 设置模糊图片
    BlurImageView *blurImage = [[BlurImageView alloc] initWithFrame:kBounds];
    blurImage.image = [UIImage imageNamed:@"12129381,1920,1080.jpg"];
    
    self.tableView.backgroundView = blurImage ;
    self.tableView.backgroundColor = [UIColor clearColor];
    // 注册cell
    [self.tableView registerClass:[MoreSpecialTableViewCell class] forCellReuseIdentifier:@"cell"];
    // 注册twoCell
    [self.tableView registerClass:[MoreSpecialTwoTableViewCell class] forCellReuseIdentifier:@"TCell"];
    
    self.tableView.separatorStyle = NO;
    
    
    //上拉加载
    __block MoreSpecialTableViewController *weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:(LORefreshViewTypeFooterDefault) refreshingBlock:^{
        [weakSelf reloadSpeail:weakSelf.NextUrl];
        [weakSelf.tableView.defaultFooter endRefreshing];
    }];
    
    //下拉刷新
    [self.tableView addRefreshWithRefreshViewType:(LORefreshViewTypeHeaderGif) refreshingBlock:^{
        weakSelf.specialArray = nil;
        [weakSelf reloadSpeail:KRotateUrl];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.gifHeader endRefreshing];
    }];
    
    
    
    //添加标题
    self.navigationItem.title = @"全部专题";
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self addToTopButton];
}

- (void)addToTopButton {
    self.toTopbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.toTopbutton.frame = CGRectMake(kWidth - 60, kHeight - 200, 40, 40);
    [self.toTopbutton setImage:[UIImage imageNamed:@"Entypo_e4af(0)_256"] forState:(UIControlStateNormal)];
    [self.toTopbutton addTarget:self action:@selector(toTop) forControlEvents:(UIControlEventTouchUpInside)];
    self.toTopbutton.alpha = 0;
    [self.view addSubview:self.toTopbutton];
    
    
    
}
- (void)toTop {
    
    self.tableView.contentOffset = CGPointMake(0, 0);
    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
    self.toTopbutton.alpha = self.tableView.contentOffset.y / 1000.0;
    self.toTopbutton.frame = CGRectMake(kWidth - 60, kHeight - 200 + self.tableView.contentOffset.y, 40, 40);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --- 数据解析 ---
- (void)reloadSpeail:(NSString *)url {
    
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *dicData = dic[@"data"];
        NSDictionary *dicPaging = dicData[@"paging"];
        self.NextUrl = dicPaging[@"next_url"];
        for (NSDictionary *collections in dicData[@"collections"]) {
            MainBoutiqueModel *model = [MainBoutiqueModel shareDictionary:collections];
            [self.specialArray addObject:model];
        }
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];   
}

- (NSMutableArray *)specialArray {
    if (!_specialArray) {
        _specialArray = [[NSMutableArray alloc] init];
    }
    return _specialArray;
}





#pragma mark --- cell设置 ---

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.specialArray.count;
}

// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        MoreSpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        if (cell == nil) {
            cell = [[MoreSpecialTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        }
        
        MainBoutiqueModel *model = self.specialArray[indexPath.row];
        [cell shareModel:model];
        
        
        return cell;
    }



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SpecialDetailsTableViewController *specialVC = [[SpecialDetailsTableViewController alloc] init];
    MainBoutiqueModel *model = [[MainBoutiqueModel alloc] init];
    model = self.specialArray[indexPath.row];
    NSString *newUrl = [KRotateDetailsUrl stringByReplacingOccurrencesOfString:@"参数" withString:[NSString stringWithFormat:@"%@",model.ID]];
    specialVC.specialDeatilsUrl = newUrl;
    [self.navigationController pushViewController:specialVC animated:YES];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return 160;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
